home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.09 Sep 88 / Basic September / Matrix stuff
Encoding:
Text File  |  1988-07-02  |  1.1 KB  |  49 lines  |  [TEXT/TRUE]

  1. ! Simultaneous Equation Solver
  2. ! Dave Kelly
  3. ! ©1988 MacTutor
  4.  
  5. LIBRARY "Fnmlib"
  6. DECLARE DEF Factrl
  7.  
  8. DEF Get_Equation_Count            !Read the number of equations
  9.     WHEN ERROR IN
  10.          LET Count=0
  11.          DO
  12.             READ value
  13.             LET Count=Count+1
  14.          LOOP
  15.     USE
  16.          RESTORE
  17.     END WHEN
  18.     LET n=0
  19.     DO
  20.        LET n=n+1
  21.     LOOP UNTIL Factrl(n)>Count
  22.     LET Count=n-1
  23.     LET Get_Equation_Count=Count
  24. END DEF
  25.  
  26. ! Start Main Program
  27. LET number_of_equations=Get_Equation_Count
  28. DIM a(2,2),b(2,2),c(2,1),x(2)
  29. ! Set up all the equations
  30. MAT READ a(number_of_equations,number_of_equations)
  31. MAT READ c(number_of_equations,1)
  32. MAT REDIM x(number_of_equations)
  33. CALL sim_solve(a,b,c,x,number_of_equations)
  34. MAT PRINT x                       ! Print the solutions
  35. DATA 1,-4,4,-1,6,-3,1,0,-1        ! equation coefficients
  36. DATA 7,0,7                        ! equation data
  37. END
  38.  
  39. SUB sim_solve(a(,),b(,),c(,),x(),number_of_equations)
  40.     IF det(a)=0 THEN EXIT SUB
  41.     FOR j=1 to number_of_equations
  42.         MAT b=a
  43.         FOR i= 1 to number_of_equations
  44.             LET b(i,j)=c(i,1)
  45.         NEXT i
  46.         LET x(j)=det(b)/det(a)
  47.     NEXT j
  48. END SUB
  49.